package com.etc.controller;

import com.etc.entity.CommentResult;
import com.etc.entity.Device;
import com.etc.service.BuildingService;
import com.etc.service.DeviceService;
import com.etc.service.ParkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class DeviceController {
    @Autowired
    private DeviceService deviceService;

    @Autowired
    private ParkService parkService;

    @Autowired
    private BuildingService buildingService;

    @RequestMapping("device/findAllDevice")
    public CommentResult findAll(@RequestParam(value = "start", required = false) Integer start,
                                 @RequestParam(value = "size", required = false) Integer size){

        List<Device> list = deviceService.findAll();
        List<Map<String,Object>> result = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            Map<String,Object> map = new HashMap<>();
            map.put("id",list.get(i).getId());
            map.put("park",parkService.findById(buildingService.findById(list.get(i).getBuilding_id()).getPark_id()).getPark_name());
            map.put("park_id",buildingService.findById(list.get(i).getBuilding_id()).getPark_id());
            map.put("building",buildingService.findById(list.get(i).getBuilding_id()).getName());
            map.put("building_id",buildingService.findById(list.get(i).getBuilding_id()).getId());
            map.put("layers",list.get(i).getLayers());
            map.put("device",list.get(i).getName());
            map.put("repair_date",list.get(i).getRepair_date());
            map.put("status",list.get(i).getStatus());
            result.add(map);
        }
        return new CommentResult(200, "success", result);

    }


    @RequestMapping("device/findByPark/{id}")
    public CommentResult findByPark(@PathVariable("id")Integer id, @RequestParam(value = "start", required = false) Integer start,
                                    @RequestParam(value = "size", required = false) Integer size) {

        List<Device> list = deviceService.findByPark(id);
        List<Map<String,Object>> result = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            Map<String,Object> map = new HashMap<>();
            map.put("id",list.get(i).getId());
            map.put("park_id",buildingService.findById(list.get(i).getBuilding_id()).getPark_id());
            map.put("park",parkService.findById(buildingService.findById(list.get(i).getBuilding_id()).getPark_id()).getPark_name());
            map.put("building",buildingService.findById(list.get(i).getBuilding_id()).getName());
            map.put("building_id",buildingService.findById(list.get(i).getBuilding_id()).getId());
            map.put("layers",list.get(i).getLayers());
            map.put("device",list.get(i).getName());
            map.put("repair_date",list.get(i).getRepair_date());
            map.put("status",list.get(i).getStatus());
            result.add(map);
        }
        return new CommentResult(200, "success", result);

    }
    @RequestMapping("device/findByBuilding/{id}")
    public CommentResult findByBuilding(@PathVariable("id")Integer id, @RequestParam(value = "start", required = false) Integer start,
                                    @RequestParam(value = "size", required = false) Integer size) {

        List<Device> list = deviceService.findByBuilding(id);
        List<Map<String,Object>> result = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            Map<String,Object> map = new HashMap<>();
            map.put("id",list.get(i).getId());
            map.put("park",parkService.findById(buildingService.findById(list.get(i).getBuilding_id()).getPark_id()).getPark_name());
            map.put("park_id",buildingService.findById(list.get(i).getBuilding_id()).getPark_id());
            map.put("building",buildingService.findById(list.get(i).getBuilding_id()).getName());
            map.put("building_id",buildingService.findById(list.get(i).getBuilding_id()).getId());
            map.put("layers",list.get(i).getLayers());
            map.put("device",list.get(i).getName());
            map.put("repair_date",list.get(i).getRepair_date());
            map.put("status",list.get(i).getStatus());
            result.add(map);
        }
        return new CommentResult(200, "success", result);

    }

    @RequestMapping("device/findByAnyCondition")
    public CommentResult findByAnyCondition(@RequestBody Map<String,Object> map, @RequestParam(value = "start", required = false) Integer start,
                                            @RequestParam(value = "size", required = false) Integer size) {


        List<Device> list = deviceService.findByAnyCondition(map);
        List<Map<String,Object>> result = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            Map<String,Object> map1 = new HashMap<>();
            map1.put("id",list.get(i).getId());
            map1.put("park",parkService.findById(buildingService.findById(list.get(i).getBuilding_id()).getPark_id()).getPark_name());
            map1.put("park_id",buildingService.findById(list.get(i).getBuilding_id()).getPark_id());
            map1.put("building",buildingService.findById(list.get(i).getBuilding_id()).getName());
            map1.put("building_id",buildingService.findById(list.get(i).getBuilding_id()).getId());
            map1.put("layers",list.get(i).getLayers());
            map1.put("device",list.get(i).getName());
            map1.put("repair_date",list.get(i).getRepair_date());
            map1.put("status",list.get(i).getStatus());
            result.add(map1);
        }
        return new CommentResult(200, "success", result);

    }

    @RequestMapping("device/findById/{id}")
    public CommentResult findById(@PathVariable("id")Integer id)
    {
        Device result = deviceService.findById(id);
        List<Map<String,Object>> list = new ArrayList<>();

            Map<String,Object> map = new HashMap<>();
            map.put("id",result.getId());
            map.put("park",parkService.findById(buildingService.findById(result.getBuilding_id()).getPark_id()).getPark_name());
            map.put("park_id",buildingService.findById(result.getBuilding_id()).getPark_id());
            map.put("building",buildingService.findById(result.getBuilding_id()).getName());
            map.put("building_id",buildingService.findById(result.getBuilding_id()).getId());
            map.put("layers",result.getLayers());
            map.put("device",result.getName());
            map.put("repair_date",result.getRepair_date());
            map.put("status",result.getStatus());
            list.add(map);

        return new CommentResult(200, "success", list);
    }

    @RequestMapping("device/findByStatus/{status}")
    public CommentResult findByStatus(@PathVariable("status") Integer status)
    {
        List<Device> list = deviceService.findByStatus(status);
        List<Map<String,Object>> result = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            Map<String,Object> map = new HashMap<>();
            map.put("id",list.get(i).getId());
            map.put("park_id",parkService.findById(buildingService.findById(list.get(i).getBuilding_id()).getPark_id()).getId());
            map.put("park",parkService.findById(buildingService.findById(list.get(i).getBuilding_id()).getPark_id()).getPark_name());
            map.put("building",buildingService.findById(list.get(i).getBuilding_id()).getName());
            map.put("building_id",buildingService.findById(list.get(i).getBuilding_id()).getId());
            map.put("layers",list.get(i).getLayers());
            map.put("device",list.get(i).getName());
            map.put("repair_date",list.get(i).getRepair_date());
            map.put("status",list.get(i).getStatus());
            result.add(map);
        }
        return new CommentResult(200, "success", result);
    }
}
